﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICSharpCode.SharpZipLib.Zip;
using COM.Web;
using ICSharpCode.SharpZipLib.GZip;
using WebApp.Web.Service;
using COM.Data;
using WebApp.Service;

namespace WebApp.Web.Controllers
{
    public class UserFeedbackController : Controller
    {
        private IUploadFileService uploadFileService = Unity.Instance.GetService<IUploadFileService>();
        private IAppPathService appPathService = Unity.Instance.GetService<IAppPathService>();
        private IRuntimeService runtimeService = Unity.Instance.GetService<IRuntimeService>();

        public ActionResult Index()
        {
            return View("Index");
        }


        [HttpPost]
        public ActionResult UploadLogFile(Guid id, string qqfilename)
        {
            try
            {
                if (Request.Files.Count != 1)
                    throw new ApplicationException("上传文件应有一个文件流.");

                HttpPostedFileBase postFile = Request.Files[0];
                if (postFile.ContentLength == 0)
                    throw new ApplicationException("空文件");

                var fileID = string.Format("{0}-{1}",id,DateTime.Now.ToString("yyyyMMdd-HHmmss-fff"));
                var fileItemID = this.uploadFileService.UploadFile("Feedback", qqfilename, Request.Files[0]);
                return Json(new { success = true, fileItemID = fileItemID,message="上传成功." }, "application/json");
            }
            catch (Exception exception)
            {
                string format = "FileUploadError:{0}={1}\r\n{2}.Message:{3}\r\nStack:{4}\r\n";
                string errorInfo = string.Format(format, this.Request.HttpMethod, this.Request.Url, exception.GetType().Name, exception.Message, exception.StackTrace);
                var logErrorService = Unity.Instance.GetService<COM.Service.ILogErrorService>();
                logErrorService.LogWebAppError(errorInfo);
                return Json(new { success = false, fileItemID=Guid.Empty, message = "上传失败!" }, "application/json");
            }

        }

    }
}
